package com.songcha.library_common.util

import android.R.attr.bitmap
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.graphics.drawable.toBitmap
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream


class BitmapUtil {
    companion object {

        @JvmStatic
        fun bitmap2Drawable(context: Context,bitmap: Bitmap):Drawable{
             return BitmapDrawable(context.resources,bitmap)
        }

        @JvmStatic
        fun drawable2Bitmap(drawable: Drawable):Bitmap{
            return drawable.toBitmap()
        }

        //TODO
        @JvmStatic
        fun compressBitmap(bitmap:Bitmap,quality:Int):Bitmap{
            val baos = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos)
            val byte = baos.toByteArray()
            val ins = ByteArrayInputStream(byte)
            val bm = BitmapFactory.decodeStream(ins)
            ins.close()
            baos.close()
            return bm
        }

        @JvmStatic
        fun view2Bitmap(view: View): Bitmap? {
            val rect = Rect().apply {
                left = 0
                top = 0
                right = view.width
                bottom = view.height
            }
            return view2Bitmap(view,rect,1f)
        }
        /**
         *
         * get view crop rect efficiently
         * @param view
         * @param crop
         * @param downscaleFactor
         * @return
         */
        @JvmStatic
        fun view2Bitmap(view: View, crop: Rect, downscaleFactor: Float): Bitmap? {
            try{
                val screenView: View = view
                val width = (crop.width() * downscaleFactor).toInt()
                val height = (crop.height() * downscaleFactor).toInt()
                val dx: Float = -crop.left * downscaleFactor
                val dy: Float = -crop.top * downscaleFactor
                if (width * height <= 0) {
                    return null
                }
                val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
                val canvas = Canvas(bitmap)
                canvas.density = ScreenUtil.getDensityDpi()
                val matrix = Matrix()
                matrix.preScale(downscaleFactor, downscaleFactor)
                matrix.postTranslate(dx, dy)
                canvas.setMatrix(matrix)
                screenView.draw(canvas)
                return bitmap
            }catch (e:Exception){
                e.printStackTrace()
                return null
            }
        }

        @JvmStatic
        fun bitmap2ByteArray(bitmap:Bitmap):ByteArray{
            val stream = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
            val byteArray: ByteArray = stream.toByteArray()
            return byteArray
        }


    }
}